<!--
  智慧社区管理系统 - 预约列表管理页面
  @author Wu.Liang
  @date 2025-01-20
  @description 服务预约列表、审核、管理
-->
<template>
  <div class="appointment-list-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <el-icon size="24" class="title-icon"><Calendar /></el-icon>
          <h2>预约列表管理</h2>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="handleAddAppointment">
            <el-icon><Plus /></el-icon>
            新增预约
          </el-button>
          <el-button @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card total-appointments">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Calendar /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.totalAppointments }}</div>
                <div class="stat-label">预约总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card pending-appointments">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.pendingAppointments }}</div>
                <div class="stat-label">待审核</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card confirmed-appointments">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Check /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.confirmedAppointments }}</div>
                <div class="stat-label">已确认</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card completed-appointments">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Finished /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.completedAppointments }}</div>
                <div class="stat-label">已完成</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="预约人">
          <el-input
            v-model="searchForm.applicantName"
            placeholder="请输入预约人姓名"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="服务类型">
          <el-select
            v-model="searchForm.serviceType"
            placeholder="请选择服务类型"
            clearable
            style="width: 150px"
          >
            <el-option label="维修服务" :value="1" />
            <el-option label="清洁服务" :value="2" />
            <el-option label="安保服务" :value="3" />
            <el-option label="其他服务" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="预约状态">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择预约状态"
            clearable
            style="width: 150px"
          >
            <el-option label="待审核" :value="0" />
            <el-option label="已确认" :value="1" />
            <el-option label="已完成" :value="2" />
            <el-option label="已取消" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="预约时间">
          <el-date-picker
            v-model="searchForm.appointmentTimeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 350px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 预约列表 -->
    <el-card class="appointment-list-card">
      <template #header>
        <div class="card-header">
          <span>预约列表</span>
          <div class="header-actions">
            <el-button type="success" size="small" @click="handleBatchConfirm">
              <el-icon><Check /></el-icon>
              批量确认
            </el-button>
            <el-button type="warning" size="small" @click="handleBatchCancel">
              <el-icon><Close /></el-icon>
              批量取消
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="appointmentList"
        v-loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="applicantName" label="预约人" width="120" />
        <el-table-column prop="phone" label="联系电话" width="130" />
        <el-table-column prop="serviceType" label="服务类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getServiceTypeTag(row.serviceType)">
              {{ getServiceTypeName(row.serviceType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="serviceDescription" label="服务描述" min-width="200" />
        <el-table-column prop="appointmentTime" label="预约时间" width="180" />
        <el-table-column prop="status" label="预约状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTag(row.status)">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="handler" label="处理人" width="120" />
        <el-table-column prop="handleTime" label="处理时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleConfirm(row)" v-if="row.status === 0">
              <el-icon><Check /></el-icon>
              确认
            </el-button>
            <el-button type="warning" size="small" @click="handleCancel(row)" v-if="row.status === 0">
              <el-icon><Close /></el-icon>
              取消
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 预约详情弹窗 -->
    <el-dialog
      v-model="appointmentDetailVisible"
      title="预约详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="预约人">{{ currentAppointment.applicantName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentAppointment.phone }}</el-descriptions-item>
        <el-descriptions-item label="服务类型">
          <el-tag :type="getServiceTypeTag(currentAppointment.serviceType)">
            {{ getServiceTypeName(currentAppointment.serviceType) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预约状态">
          <el-tag :type="getStatusTag(currentAppointment.status)">
            {{ getStatusName(currentAppointment.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预约时间">{{ currentAppointment.appointmentTime }}</el-descriptions-item>
        <el-descriptions-item label="处理人">{{ currentAppointment.handler || '未处理' }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ currentAppointment.handleTime || '未处理' }}</el-descriptions-item>
        <el-descriptions-item label="服务地址">{{ currentAppointment.serviceAddress }}</el-descriptions-item>
        <el-descriptions-item label="服务描述" :span="2">{{ currentAppointment.serviceDescription }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentAppointment.remark || '暂无备注' }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <el-button @click="appointmentDetailVisible = false">关闭</el-button>
        <el-button type="success" @click="handleConfirm(currentAppointment)" v-if="currentAppointment.status === 0">确认预约</el-button>
        <el-button type="warning" @click="handleCancel(currentAppointment)" v-if="currentAppointment.status === 0">取消预约</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  Calendar,
  Plus,
  Refresh,
  Clock,
  Check,
  Search,
  Close,
  View,
  Finished
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const appointmentDetailVisible = ref(false)
const selectedAppointments = ref<any[]>([])

// 搜索表单
const searchForm = reactive({
  applicantName: '',
  serviceType: undefined as number | undefined,
  status: undefined as number | undefined,
  appointmentTimeRange: [] as string[]
})

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

// 统计数据
const statistics = reactive({
  totalAppointments: 156,
  pendingAppointments: 23,
  confirmedAppointments: 89,
  completedAppointments: 44
})

// 当前预约详情
const currentAppointment = ref<any>({})

// 预约列表数据
const appointmentList = ref([
  {
    id: 1,
    applicantName: '张三',
    phone: '13800138001',
    serviceType: 1,
    serviceDescription: '水管漏水维修',
    appointmentTime: '2025-01-20 15:30:00',
    status: 0,
    handler: null,
    handleTime: null,
    serviceAddress: '1号楼101室',
    remark: '紧急维修'
  },
  {
    id: 2,
    applicantName: '李四',
    phone: '13800138002',
    serviceType: 2,
    serviceDescription: '室内清洁服务',
    appointmentTime: '2025-01-21 09:00:00',
    status: 1,
    handler: '王师傅',
    handleTime: '2025-01-20 16:00:00',
    serviceAddress: '2号楼203室',
    remark: '定期清洁'
  },
  {
    id: 3,
    applicantName: '王五',
    phone: '13800138003',
    serviceType: 3,
    serviceDescription: '安保巡逻服务',
    appointmentTime: '2025-01-22 20:00:00',
    status: 2,
    handler: '保安队长',
    handleTime: '2025-01-20 17:00:00',
    serviceAddress: '小区内',
    remark: '夜间巡逻'
  },
  {
    id: 4,
    applicantName: '赵六',
    phone: '13800138004',
    serviceType: 4,
    serviceDescription: '其他服务需求',
    appointmentTime: '2025-01-23 14:00:00',
    status: 3,
    handler: '客服小王',
    handleTime: '2025-01-20 18:00:00',
    serviceAddress: '3号楼305室',
    remark: '客户取消'
  },
  {
    id: 5,
    applicantName: '孙七',
    phone: '13800138005',
    serviceType: 1,
    serviceDescription: '电路故障维修',
    appointmentTime: '2025-01-24 10:00:00',
    status: 0,
    handler: null,
    handleTime: null,
    serviceAddress: '1号楼102室',
    remark: '电路问题'
  }
])

// 获取服务类型标签样式
const getServiceTypeTag = (type: number): 'primary' | 'success' | 'warning' | 'info' => {
  const typeMap: Record<number, 'primary' | 'success' | 'warning' | 'info'> = {
    1: 'primary',  // 维修服务
    2: 'success',  // 清洁服务
    3: 'warning',  // 安保服务
    4: 'info'      // 其他服务
  }
  return typeMap[type] || 'info'
}

// 获取服务类型名称
const getServiceTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '维修服务',
    2: '清洁服务',
    3: '安保服务',
    4: '其他服务'
  }
  return typeMap[type] || '未知类型'
}

// 获取状态标签样式
const getStatusTag = (status: number): 'warning' | 'success' | 'info' | 'danger' => {
  const statusMap: Record<number, 'warning' | 'success' | 'info' | 'danger'> = {
    0: 'warning',  // 待审核
    1: 'success',  // 已确认
    2: 'info',     // 已完成
    3: 'danger'    // 已取消
  }
  return statusMap[status] || 'warning'
}

// 获取状态名称
const getStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: '待审核',
    1: '已确认',
    2: '已完成',
    3: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadAppointmentList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    applicantName: '',
    serviceType: undefined,
    status: undefined,
    appointmentTimeRange: []
  })
  handleSearch()
}

// 加载预约列表
const loadAppointmentList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    pagination.total = appointmentList.value.length
  }, 500)
}

// 新增预约
const handleAddAppointment = () => {
  showMessage({ type: 'info', message: '新增预约功能开发中' })
}

// 刷新
const handleRefresh = () => {
  loadAppointmentList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

// 查看预约详情
const handleView = (row: any) => {
  currentAppointment.value = { ...row }
  appointmentDetailVisible.value = true
}

// 确认预约
const handleConfirm = (row: any) => {
  showMessage({ type: 'info', message: '确认预约功能开发中' })
}

// 取消预约
const handleCancel = (row: any) => {
  showMessage({ type: 'info', message: '取消预约功能开发中' })
}

// 批量确认
const handleBatchConfirm = () => {
  if (selectedAppointments.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要确认的预约' })
    return
  }
  showMessage({ type: 'info', message: '批量确认功能开发中' })
}

// 批量取消
const handleBatchCancel = () => {
  if (selectedAppointments.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要取消的预约' })
    return
  }
  showMessage({ type: 'info', message: '批量取消功能开发中' })
}

// 选择变化处理
const handleSelectionChange = (selection: any[]) => {
  selectedAppointments.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadAppointmentList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  loadAppointmentList()
}

// 页面初始化
onMounted(() => {
  loadAppointmentList()
})
</script>

<style scoped lang="scss">
.appointment-list-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-title {
        display: flex;
        align-items: center;
        color: white;

        .title-icon {
          margin-right: 12px;
        }

        h2 {
          margin: 0;
          font-size: 24px;
          font-weight: 600;
        }
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }

  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 8px;

        .stat-icon {
          margin-right: 16px;
          padding: 12px;
          border-radius: 8px;
          color: white;
        }

        .stat-info {
          .stat-number {
            font-size: 28px;
            font-weight: bold;
            line-height: 1;
            margin-bottom: 4px;
          }

          .stat-label {
            font-size: 14px;
            color: #666;
          }
        }
      }

      &.total-appointments .stat-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      &.pending-appointments .stat-icon {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      &.confirmed-appointments .stat-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      &.completed-appointments .stat-icon {
        background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      }
    }
  }

  .search-card {
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }

  .appointment-list-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
      font-size: 16px;

      .header-actions {
        display: flex;
        gap: 8px;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;
    }
  }
}
</style> 